package room

import (
	"errors"
	"server/common"

	"github.com/mitchellh/mapstructure"
	"github.com/sirupsen/logrus"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) checkStartPublishAckData(data common.SFUStartPublishAckData) error {
	if data.RoomId == "" {
		return errors.New("room ID cannot be empty")
	}
	if data.UserId == "" {
		return errors.New("user ID cannot be empty")
	}
	if data.Media.MediaId == "" {
		return errors.New("media ID cannot be empty")
	}
	return nil
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) handleStartPublishAck(msg common.Message, from common.ServiceInfo,
	connId string) error {
	LOG().WithFields(logrus.Fields{
		"msg":    common.ToJson(msg),
		"from":   from,
		"connId": connId,
	}).Info("Handling SFU Start Publish Ack")

	var data common.SFUStartPublishAckData
	if err := mapstructure.Decode(msg.Data, &data); err != nil {
		LOG(msg.Rid).WithError(err).Error("Failed to decode SFU Start Publish Ack data")
		return err
	}

	if err := s.checkStartPublishAckData(data); err != nil {
		LOG(msg.Rid).WithError(err).Error("Invalid SFU Start Publish Ack data")
		return nil
	}

	LOG(msg.Rid).Info("SFU Start Publish Ack data is valid")

	return nil
}
